package co.com.smart.eagle.client.view.impl.smartsimplepager;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasRows;
import com.google.gwt.view.client.Range;

public class SmartSimplePager  extends SimplePager implements IsWidget{

	private final HTML preLabel = new HTML();
	
	@UiConstructor
	public SmartSimplePager() {
		super(SimplePager.TextLocation.CENTER,(SimplePager.Resources) GWT.create(SmartSimplePagerResources.class),false,0,true);
		Widget w = this.getWidget();
		HorizontalPanel layout = (HorizontalPanel) w;
		preLabel.setHTML("");
		preLabel.setText("");
		layout.insert(preLabel,0);
	}
	
	@Override
	protected String createText() {
	    // Default text is 1 based.
	    HasRows display = getDisplay();
	    Range range = display.getVisibleRange();
	    int pageStart = range.getStart() + 1;
	    int pageSize = range.getLength();
	    int dataSize = display.getRowCount();
	    int endIndex = Math.min(dataSize, pageStart + pageSize - 1);
	    endIndex = Math.max(pageStart, endIndex);
	    int pagerSize = (dataSize/pageSize);
	    if((pageSize*pagerSize)<dataSize)
	    {
	    	pagerSize = pagerSize + 1;
	    }
	    int pagerStart = (endIndex/pageSize);
	    if(endIndex == dataSize){
	    	pagerStart = pagerSize;
	    }
	    if(dataSize == 0){
	    	this.preLabel.setText(SmartSimplePagerMessages.INSTANCE.getPreLabelNoData());	
	    }else{
	    	this.preLabel.setText(SmartSimplePagerMessages.INSTANCE.getPreLabel(pageStart, endIndex, dataSize));
	    }
	   	return SmartSimplePagerMessages.INSTANCE.getLabel(pagerStart, pagerSize);
	}
	
	@Override	
	public void previousPage() {
		HasRows display = getDisplay();
		if (display != null) {
			Range range = display.getVisibleRange();
		    int pageStart = range.getStart() + 1;
		    int pageSize = range.getLength();
		    int dataSize = display.getRowCount();
			int endIndex = Math.min(dataSize, pageStart + pageSize - 1);
			int pagerSize = (dataSize/pageSize);
		    if((pageSize*pagerSize)<dataSize)
		    {
		    	pagerSize = pagerSize + 1;
		    }
		    int pagerStart = (endIndex/pageSize);
		    if(endIndex == dataSize){
		    	pagerStart = pagerSize;
		    }	    
		    pagerStart = pagerStart - 2;
	        setPageStart(pagerStart * range.getLength());
	    }
		
	}
	
	@Override
	public void nextPage(){
		HasRows display = getDisplay();
		if (display != null) {
			Range range = display.getVisibleRange();
		    int pageStart = range.getStart() + 1;
		    int pageSize = range.getLength();
		    int dataSize = display.getRowCount();
			int endIndex = Math.min(dataSize, pageStart + pageSize - 1);
			int pagerSize = (dataSize/pageSize);
		    if((pageSize*pagerSize)<dataSize)
		    {
		    	pagerSize = pagerSize + 1;
		    }
		    int pagerStart = (endIndex/pageSize);
		    
		    if(endIndex == dataSize){
		    	pagerStart = pagerSize;
		    }
		    
		    if(pagerStart < (pagerSize - 1))
		    {
		    	setPageStart(pagerStart * range.getLength());
		    }
		    else
		    {
		    	lastPage();
		    }
	    }
	}
}
